import _ from 'lodash'
const mutations = {
  addShopcart(state, orderLine) {
    state.orderLines.set(orderLine.productId, orderLine);
    // 监听到orderLines的变化
    state.orderLines = _.clone(state.orderLines)
    state.orders = []
    for (let orderLine of state.orderLines.values()) {
      if (orderLine.number) {
        state.orders.push(orderLine);
      }

    }
  },
  clearShopcart(state) {
    state.orderLines.clear();
    state.orders = [];
    state.orderLines = _.clone(state.orderLines)
  }
}
const getters = {
  total(state) {
    let result = 0;
    for (let orderLine of state.orderLines.values()) {
      result += orderLine.price * orderLine.number;
    }
    return result;
  }
}
const state = {
  orders: [],
  orderLines: new Map()
}

export default {
  namespaced: true,
  state,
  mutations,
  getters,
}